#!/bin/bash
#
##############################################################################
#                                                                            #
#                      Pi-Star BrandMeister API Tool                         #
#                                                                            #
#     Version 1.10, Code, Design and Development by Andy Taylor (MW0MWZ).    #
#                                                                            #
#    Make it simple to link / unlink reflectors from the CLI on Pi-Star.     #
#    Added the option to manage static/dynamic TalkGroups and to be able     #
#                        to drop the current QSO.                            #
#                                                                            #
##############################################################################
#
if [ "$(id -u)" != "0" ]; then
  echo -e "You need to be root to run this command...\n"
  exit 1
fi

# Setup some variables
APIKeyFile=/etc/bmapi.key

# Figure out the DMR ID
DMRNetHost=$(sed -n '/^\[DMR Network\]/,/^\[/p' /etc/mmdvmhost | grep "^Address=" | head -n 1 | awk -F "=" '{print $2}')
if [[ "${DMRNetHost}" == "127.0.0.1" ]]; then
        # Using DMRGateway
        DMRID=$(sed -n '/^\[DMR Network 1\]/,/^\[/p' /etc/dmrgateway | grep "^Id" | awk -F "=" '{print $2}')
else
        # Using MMDVMHost
        DMRMMDVMHostID=$(sed -n '/^\[DMR\]/,/^\[/p' /etc/mmdvmhost | grep "^Id" | awk -F "=" '{print $2}')
        if [[ ${DMRMMDVMHostID} ]]; then
                # ID Specified in the DMR section
                DMRID=${DMRMMDVMHostID}
        else
                # No ID in the DMR Section
                DMRID=$(grep -m 1 '^Id=' /etc/mmdvmhost | awk -F "=" '{print $2}')
        fi
fi
# Fix edge case where there is some additional chars in the DMR ID Fields (DF9FZ)
DMRID=${DMRID//$'\r'/}

if [ -z "$1" ]
then
	echo "NOTE: In the below example commands, please replace {TG Number} with"
	echo "the number of the Talkgroup you wish to add/remove. For example 12345."
	echo ""
	echo "Similarly the {TimeSlot} should be replaced with your target time"
	echo "slot (0, 1, 2), for hotspots it is \"0\" (you can also omit the TimeSlot"
	echo "value), for duplex repeaters it will be either \"1\" for TS1 or \"2\" for TS2."
	echo ""
	echo "Reflector numbers are from 4000 to 5000"
	echo ""
	echo "To add a static TG:            pistar-bmapi addtg {TG Number} {TimeSlot Number}"
	echo "To remove a static TG:         pistar-bmapi deltg {TG Number} {TimeSlot Number}"
	echo "To drop all Dynamic TGs:       pistar-bmapi dropdyn {TimeSlot Number}"
	echo "To drop the current QSO:       pistar-bmapi dropqso {TimeSlot Number}"
	echo ""
	exit 0
fi

# Get the stored BM API Key
if [ -f ${APIKeyFile} ]; then
	if [[ $(wc -c ${APIKeyFile} | awk '{print $1}') -gt 200 ]]; then
		echo -e "Using BM API v2"
		APIURL="https://api.brandmeister.network/v2/device/"
		APIKey=$(grep -m 1 'apikey=' ${APIKeyFile} | awk -F "=" '/apikey/ {print $2}')
		APIVer="2"
	else
		echo -e "Using BM API v1.0"
		echo -e "Please obtain a new BM API Key, v1 keys are no longer supported!"
		exit 0
	fi
else
	echo "Unable to find your BrandMesiter API Key, no API commands available."
	exit 0
fi

case ${1} in
apikey)
	echo "Your BrandMeister API Key is: ${APIKey}"
	exit 0
;;
dmrid)
	echo "Your DMR ID is: ${DMRID}"
	exit 0
;;
addtg)
	# Check that the variable for TalkGroup has been passed
	if [ -z "$2" ]
	then
		echo "No TG Specfified"
		exit 0
	else
		targetTG=$2
	fi
	# Check that the variable to TimeSlot has been passed
	if [ -z "$3" ]
	then
		TS="0"
	else
		TS=$3
	fi

	APIURL="${APIURL}${DMRID}/talkgroup"
	APIDATA="{ \"slot\": ${TS}, \"group\": ${targetTG} }"
	TYPE="POST"
	REQUEST="Add TG ${targetTG} to TimeSlot ${TS} for DMR ID ${DMRID}"
;;
deltg)
	# Check that the variable for TalkGroup has been passed
	if [ -z "$2" ]
	then
		echo "No TG Specfified"
		exit 0
	else
		targetTG=$2
	fi
	# Check that the variable to TimeSlot has been passed
	if [ -z "$3" ]
	then
		TS="0"
	else
		TS=$3
	fi

	APIURL="${APIURL}${DMRID}/talkgroup/${TS}/${targetTG}"
	TYPE="DELETE"
	REQUEST="Delete TG ${targetTG} from TimeSlot ${TS} for DMR ID ${DMRID}"
;;
dropdyn)
	# Check that the variable to TimeSlot has been passed
	if [ -z "$2" ]
	then
		TS="0"
	else
		TS=$2
	fi

	APIURL="${APIURL}${DMRID}/action/dropDynamicGroups/${TS}"
	TYPE="GET"
	REQUEST="Drop all Dynamic TG on TimeSlot ${TS} for DMR ID ${DMRID}"
;;
dropqso)
	# Check that the variable to TimeSlot has been passed
	if [ -z "$2" ]
	then
		TS="0"
	else
		TS=$2
	fi

	APIURL="${APIURL}${DMRID}/action/dropCallRoute/${TS}"
	TYPE="GET"
	REQUEST="Drop Active QSO on TimeSlot ${TS} for DMR ID ${DMRID}"
;;
*)
	echo "ERROR: Unknown Command Specified"
	exit 0
;;
esac

# Do the magic
echo " Request to BrandMesiter API: ${REQUEST}"
RESULT=$(curl -s -X "${TYPE}" "${APIURL}" -A "Pi-Star cli tool for: ${DMRID}" -H "accept: application/json" -H "Authorization: Bearer ${APIKey}" -H "Content-Type: application/json" -d "${APIDATA}" > /dev/null 2>&1 && echo OK || echo ERROR)
echo "Answer from BrandMesiter API: ${RESULT}"
echo ""
exit 0
